<?php

namespace App\Jobs\RongCloud;

use App\Exceptions\BasicException;
use App\Jobs\Job;
use App\Repositories\LogRepository;
use App\Services\Tools\HelperService;

class BroadcastPushJob extends Job
{
    public $senderId;

    public $content;

    public $type;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($senderId, $content, $type = 'RC:CmdMsg')
    {
        $this->senderId = $senderId;
        $this->content = $content;
        $this->type = $type;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        /**
         * @var $rcService \App\Services\Tools\RongCloudService
         */
        $rcService = app('rongcloud');
        $micTime = HelperService::microtime_float();
        try {
            // 处理一些 是否 大于多少条是的就进行不推送  6 条 把  就睡眠  在来个定时器保险起见 定时器推送失败的 推送
            // $redis// 处理吧
            if (app('model_broadcast_ry_log')->delay()) {
                sleep(1);
            }
            $result = $rcService->broadcastPush($this->senderId, $this->content, $this->type);
            if ($result['code'] != 200) {
                $result = $rcService->broadcastPush($this->senderId, $this->content, $this->type);
                if ($result['code'] != 200) {
                    throw new BasicException($result['code'], $result['msg']);
                }
            }
        } catch (\Exception $e) {
            $result = [
                'code' => 500,
                'msg' => "msg" . $e->getMessage() . " code:" . $e->getCode() . " line:" . $e->getLine() . ' file:' . $e->getFile(),
            ];
        }
        try {
            app('model_broadcast_ry_log')->addLumenQueueLogData($this->senderId, $this->content, $this->type, $result, $micTime);
        } catch (\Exception $e) {

        }

    }
}
